/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.service.beans;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * This class corresponds to the "ressource_request" element of the requests_config.xml file.
 * It allows to associate search form fields with searched business bean fields.
 */
public class JBRessourceRequest implements java.io.Serializable
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBRessourceRequest.class.getName());

    /** Searched business bean resource name (defined in profiles_config.xml). */
    private String name;

    /** List of JBFormField. */
    private Map formMap = new HashMap();

    /** Creates new JBRessourceRequest. */
    public JBRessourceRequest()
    {
    }

    /**
     * Returns the searched business bean resource name (defined in profiles_config.xml).
     * @return resource name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Sets the the searched business bean resource name (defined in profiles_config.xml).
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
    * Returns the list of form fields.
    * @return Map with name as key, JBFormField as value
    */
    public Map getFormMap()
    {
        return formMap;
    }

    /**
     * Adds a form field.
     *
     * @param form form field
     */
    public void addForm(JBFormField form)
    {
        formMap.put(form.getName().trim(), form);
    }

    /**
     * Gets a search form field from its name.
     *
     * @param aname form field name
     *
     * @return form field
     */
    public JBFormField findFormByName(String aname)
    {
        _logger.debug("findFormByName name : " + aname);

        String key = aname.trim();

        return (JBFormField) formMap.get(key);
    }

    /**
     * Displays object definition.
     *
     * @return String formatted object definition.
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("      name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));

        if (formMap != null)
        {
            buf.append("   Map components of formMap = ");
            buf.append(System.getProperty("line.separator"));
            it = formMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("   ...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method
}
